/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetradar.multibackstack.sample.fraction;

import com.jetradar.multibackstack.sample.ResourceTable;
import com.jetradar.multibackstack.sample.provider.ItemProvider;
import com.jetradar.multibackstack.sample.slice.MainAbilitySlice;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;

import ohos.agp.components.*;
import ohos.agp.utils.Color;


import java.util.ArrayList;
import java.util.List;

/**
 * SearchFraction
 *
 * @since 2021-06-10
 */
public class SearchFraction extends Fraction {
    private static final int NUM_10 = 10;
    private ListContainer listContainer;
    private ItemProvider itemProvider;
    private MainAbilitySlice context;
    private Text text;
    private List<String> data;

    /**
     * SearchFraction
     *
     * @param abilitySlice context
     */
    public SearchFraction(MainAbilitySlice abilitySlice) {
        this.context = abilitySlice;
    }

    /**
     * onComponentAttached
     *
     * @param scatter   scatter
     * @param container container
     * @param intent    intent
     * @return Component
     */
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_search_fraction, null, false);
        listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_search_list);
        text = (Text) component.findComponentById(ResourceTable.Id_search_fraction_text);
        return component;
    }

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        data = getData();
        itemProvider = new ItemProvider(data, context);
        listContainer.setItemProvider(itemProvider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer container, Component component, int i, long l) {
                context.addAction(SearchFraction.this, i);
                click(i);
            }
        });
    }

    /**
     * showAll
     */
    public void showAll() {
        listContainer.setVisibility(Component.VISIBLE);
        text.setVisibility(Component.HIDE);
    }


    private void click(int i) {
        listContainer.setVisibility(Component.HIDE);
        text.setVisibility(Component.VISIBLE);
        text.setText(data.get(i));
        text.setTextColor(new Color(Color.getIntColor("#A2A2A2")));
    }

    private void unClick() {
        context.initTitle();
        listContainer.setVisibility(Component.VISIBLE);
        text.setVisibility(Component.HIDE);
    }

    /**
     * setItem
     *
     * @param position position
     */
    public void setItem(int position) {
        if (position == -1) {
            unClick();
        } else {
            click(position);
        }
    }

    private List<String> getData() {
        List<String> list = new ArrayList<>();
        for (int index = 1; index <= NUM_10; index++) {
            list.add("Search " + index);
        }
        return list;
    }
}
